/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadpo.insertproduct;

import br.com.sales.hanserver.action.hancadpo.getdatagrid.GetDataProductGridAction;
import br.com.sales.hanserver.action.hancadpo.insertproduct.query.InsertProductQuery;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class InsertProductAction extends InsertProductQuery {

    public InsertProductAction() {
    }

    public Map insertProduct(Map fields) {
        Long sequence = 0L;
        try {
            sequence = Long.parseLong(fields.get("SEQ").toString());
            if (sequence > 0) {
                updateProduct(fields.get("PRODUCT").toString(),
                        !isNull(fields.get("BUY_VALUE")) ? new Double(fields.get("BUY_VALUE").toString()) : null,
                        new Double(fields.get("UNIT_VALUE").toString()),
                        !isNull(fields.get("PROMOTIONAL_VALUE")) ? new Double(fields.get("PROMOTIONAL_VALUE").toString()) : null,
                        !isNull(fields.get("PROMOTIONAL_VALUE")) ? new Double(fields.get("PROFIT_MARGIN").toString()) : null,
                        Integer.parseInt(fields.get("COUNT_STOCK").toString()),
                        !isNull(fields.get("COUNT_MIN_STOCK")) ? Integer.parseInt(fields.get("COUNT_MIN_STOCK").toString()) : null,
                        !isNull(fields.get("COUNT_MAX_STOCK")) ? Integer.parseInt(fields.get("COUNT_MAX_STOCK").toString()) : null,
                        (String) fields.get("OBSERVATION"),
                        !isNull(fields.get("PROVIDER_ID")) ? new Long((String) fields.get("PROVIDER_ID")) : null,
                        (String) fields.get("STOCK"),
                        new Timestamp(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(fields.get("DATE").toString()).getTime()),
                        sequence);
            } else {
                insertProduct(fields.get("PRODUCT").toString(),
                        !isNull(fields.get("BUY_VALUE")) ? new Double(fields.get("BUY_VALUE").toString()) : null,
                        new Double(fields.get("UNIT_VALUE").toString()),
                        !isNull(fields.get("PROMOTIONAL_VALUE")) ? new Double(fields.get("PROMOTIONAL_VALUE").toString()) : null,
                        !isNull(fields.get("PROFIT_MARGIN")) ? new Double(fields.get("PROFIT_MARGIN").toString()) : null,
                        Integer.parseInt(fields.get("COUNT_STOCK").toString()),
                        !isNull(fields.get("COUNT_MIN_STOCK")) ? Integer.parseInt(fields.get("COUNT_MIN_STOCK").toString()) : null,
                        !isNull(fields.get("COUNT_MAX_STOCK")) ? Integer.parseInt(fields.get("COUNT_MAX_STOCK").toString()) : null,
                        (String) fields.get("OBSERVATION"),
                        !isNull(fields.get("PROVIDER_ID")) ? new Long((String) fields.get("PROVIDER_ID")) : null,
                        (String) fields.get("STOCK"),
                        new Timestamp(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(fields.get("DATE").toString()).getTime()));
            }
        } catch (Exception ex) {
            Logger.getLogger(InsertProductAction.class.getName()).log(Level.ALL, null, ex);
        }
        return new GetDataProductGridAction().getLastProduct(sequence);
    }

    private boolean isNull(Object isNull) {
        return "".equals(isNull) || isNull == null;
    }

}
